package com.cjh.mybatis08.executor;

import com.cjh.mybatis08.executor.statement.StatementHandler;
import com.cjh.mybatis08.mapping.BoundSql;
import com.cjh.mybatis08.mapping.MappedStatement;
import com.cjh.mybatis08.session.Configuration;
import com.cjh.mybatis08.session.ResultHandler;
import com.cjh.mybatis08.transaction.Transaction;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @Descripton 简单执行器
 * @Author cjh
 * @Date 2023/6/1 10:27
 */
public class SimpleExecutor extends BaseExecutor {
    public SimpleExecutor(Configuration configuration, Transaction transaction) {
        super(configuration, transaction);
    }

    @Override
    protected <E> List<E> doQuery(MappedStatement mappedStatement, Object parameter, ResultHandler resultHandler, BoundSql boundSql) {
        try {
            Configuration configuration1 = mappedStatement.getConfiguration();
            StatementHandler handler = configuration1.newStatementHandler(this, mappedStatement, parameter
                    , resultHandler, boundSql);
            Connection connection = transaction.getConnection();
            Statement stmt = handler.prepare(connection);
            handler.parameterize(stmt);
            return handler.query(stmt, resultHandler);

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
